local MobaShop = MClass("MobaShop")
local ScrollViewExtend = require("app.widget.ScrollViewExtend")
local ShopItem = require("app.battleV3.ui.objs.ShopItem")
local TabCountMax = 3

local meleeShopTypes =
{
    Enums.ShopItemType.Skill,
    Enums.ShopItemType.Buff,
}

local zombieShopTypes =
{
    Enums.ShopItemType.MercenaryJunior,
    -- Enums.ShopItemType.MercenaryMedium,
    -- Enums.ShopItemType.MercenarySenior,
}

local zombieShopTitles =
{
    "zombieShopTitle1",
    "zombieShopTitle2",
    "zombieShopTitle3",
}

local string_format = string.format

function MobaShop:ctor(battleType, shopPageIndex)
    self.tabsGroups = {}
    self.battleType = battleType
    -- local tabCount = 0
    if battleType == Enums.EBattleType.BtType_Melee then
        -- tabCount = #meleeShopTypes
        self.shopTypes = meleeShopTypes
        self.shopTitles = {}
    elseif battleType == Enums.EBattleType.BtType_Zombie then
        -- tabCount = #zombieShopTypes
        self.shopTypes = zombieShopTypes
        self.shopTitles = zombieShopTitles
    end
    for i = 1, TabCountMax do
        local btn = self:findChild("Panel/Panel_tab/selected_btn_"..i)
        self.tabsGroups[i] = btn
        if self.shopTypes[i] ~= nil then
            btn:setVisible(true)
            btn:setTouchEnabled(true)
            btn:addClickEventListener(function(sender)
        	    self.tabsGroups:setIndex(i)
    	    end)
            if self.shopTitles[i] ~= nil then
                btn:findChild("name_1"):setString(L(self.shopTitles[i]))
                btn:findChild("name_2"):setString(L(self.shopTitles[i]))
            end
        else
            btn:setVisible(false)
        end
    end
    self.scrollView = ScrollViewExtend.create(self:findChild("Panel/ScrollView"), {class = ShopItem, })
    self:init()
    self.tabsGroups:setIndex(shopPageIndex)
	
	self.tips = self:findChild("Panel/tips")
	self.tips:setVisible(false)
end

function MobaShop:show(bShow)
    self:setVisible(bShow)
end

function MobaShop:init()
	self:initTabs()
	--设置
    local function touchEvent_close(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:show(false)
        end
    end
    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(self:findChild("Panel"), function()
        self:show(false)
    end)

    local function onNodeEvent(event)
        if "enter" == event then
        elseif "exit" == event then
        end
    end
    self:registerScriptHandler(onNodeEvent)
	
	-- 刷新鱼人商店
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_REFRESH_ZOMBIE_SHOP_TIPS, handler(self, MobaShop.setShopTips))
end

function MobaShop:setShopPageIndex(shopPageIndex)
    self.tabsGroups:setIndex(shopPageIndex)
end

function MobaShop:setBattleType(battleType)
    self.battleType = battleType
end

function MobaShop:setShopTips(num)
	if not self.tips:isVisible() then
		self.tips:setVisible(true)
	end
	self.tips:setString(string_format(L("zombieShopLimitTips"), num, Constant.Zombies_Max_Follow_Num))
end

function MobaShop:initTabs()
--    print("=====> initTabs")
	local callback = function(index)
        local itemIds = battle.BattleConfig:getInstance():getItemListByType(self.shopTypes[index], self.battleType)
--        dump(itemIds, "itemIds", 10)
        local data = {}
        for _, itemId in ipairs(itemIds) do
            table.insert(data, {tid = itemId, battleType = self.battleType})
        end
--        dump(data, "data", 10)
        self.scrollView:showDataList(data, 0)
    end
    setmetatable(self.tabsGroups,{__index = {
        setIndex = function(tabs,index)
            for _index,tab in pairs(tabs) do
                tab:setBright(not (_index == index))
                local name1 = tab:findChild("name_1")
                local name2 = tab:findChild("name_2")
                name1:setVisible(not(_index == index))
                name2:setVisible((_index == index))
            end
            callback(index)
        end
    }})
end

return MobaShop